package com.gfxy.modules.common.service.excel;

import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.result.ExcelImportResult;
import org.jeecgframework.poi.excel.imports.sax.SaxReadExcel;
import org.jeecgframework.poi.excel.imports.sax.parse.ISaxRowRead;
import org.jeecgframework.poi.handler.inter.IExcelReadRowHanlder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author: lijixin 2021/7/19 12:34 下午
 */
public class PlantExcelImportUtil {
    private static final Logger logger = LoggerFactory.getLogger(PlantExcelImportUtil.class);

    private PlantExcelImportUtil() {
    }

    public static <T> List<T> importExcel(File file, Class<?> pojoClass, ImportParams params) {
        FileInputStream in = null;
        List result = null;

        try {
            in = new FileInputStream(file);
            result = (new PlantExcelImportServer()).importExcelByIs(in, pojoClass, params).getList();
        } catch (Exception var14) {
            logger.error(var14.getMessage(), var14);
        } finally {
            try {
                in.close();
            } catch (IOException var13) {
                logger.error(var13.getMessage(), var13);
            }

        }

        return result;
    }

    public static <T> List<T> importExcel(InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception {
        return (new PlantExcelImportServer()).importExcelByIs(inputstream, pojoClass, params).getList();
    }

    public static <T> ExcelImportResult<T> importExcelVerify(InputStream inputstream, Class<?> pojoClass, ImportParams params) throws Exception {
        return (new PlantExcelImportServer()).importExcelByIs(inputstream, pojoClass, params);
    }

    public static <T> ExcelImportResult<T> importExcelVerify(File file, Class<?> pojoClass, ImportParams params) {
        FileInputStream in = null;

        try {
            in = new FileInputStream(file);
            ExcelImportResult var4 = (new PlantExcelImportServer()).importExcelByIs(in, pojoClass, params);
            return var4;
        } catch (Exception var14) {
            logger.error(var14.getMessage(), var14);
        } finally {
            try {
                in.close();
            } catch (IOException var13) {
                logger.error(var13.getMessage(), var13);
            }

        }

        return null;
    }

    public static <T> List<T> importExcelBySax(InputStream inputstream, Class<?> pojoClass, ImportParams params) {
        return (new SaxReadExcel()).readExcel(inputstream, pojoClass, params, (ISaxRowRead)null, (IExcelReadRowHanlder)null);
    }

    public static void importExcelBySax(InputStream inputstream, Class<?> pojoClass, ImportParams params, IExcelReadRowHanlder hanlder) {
        (new SaxReadExcel()).readExcel(inputstream, pojoClass, params, (ISaxRowRead)null, hanlder);
    }

    public static <T> List<T> importExcelBySax(InputStream inputstream, ISaxRowRead rowRead) {
        return (new SaxReadExcel()).readExcel(inputstream, (Class)null, (ImportParams)null, rowRead, (IExcelReadRowHanlder)null);
    }
}
